package com.eos.control.action.customer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eos.business.service.SysProductBusinessService;
import com.eos.common.tool.ConstantTool;
import com.eos.common.tool.ObjectUtils;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.ProductBrand;
import com.eos.orm.mybatis.model.ProductModel;
import com.eos.orm.mybatis.model.SysProduct;

public class EditCategoryAction extends AbstractBaseAction {
	private static final long serialVersionUID = 2152355399294042378L;
	private Integer modelId;
	private List<SysProduct> productList;
	private String brandOfProductJson;
	private ProductModel proModel;
	
	public void setModelId(Integer modelId) {
		this.modelId = modelId;
	}

	public List<SysProduct> getProductList() {
		return productList;
	}

	public String getBrandOfProductJson() {
		return brandOfProductJson;
	}

	public ProductModel getProModel() {
		return proModel;
	}

	@Override
	public String execute()  {
		 try {
			SysProductBusinessService spbs = new SysProductBusinessService();
			SysProduct product = new SysProduct();
			product.setDealFlag(ConstantTool.DEAL_FALG_OK);
			product.setStatus(ConstantTool.STATUS_OK);
			productList = spbs.getSysProductExample(product);
			ProductBrand brand = new ProductBrand();
			brand.setDealFlag(ConstantTool.DEAL_FALG_OK);
			brand.setStatus(ConstantTool.STATUS_OK);
			List<ProductBrand> brandList = spbs.getProductBrandExample(brand);
			brandOfProductJson= getProductBrandJson(productList, brandList);
			proModel = spbs.getProductModelPrimaryKey(modelId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	private String getProductBrandJson(List<SysProduct> proList, List<ProductBrand> brandList) {
		if (null != proList && proList.size() > 0 && null != brandList && brandList.size()>0) {
			Map<String, List<ProductBrand>> map = new HashMap<String, List<ProductBrand>>();
			for (SysProduct pro : proList) {
				for (ProductBrand brand : brandList) {
					int proId = pro.getId();
					if (brand.getProductId() == proId) {
						if (map.containsKey("pro_" + proId)) {
							map.get("pro_" + proId).add(brand);
						} else {
							List<ProductBrand> bList = new ArrayList<ProductBrand>();
							bList.add(brand);
							map.put("pro_"+proId, bList);
						}
					}
				}
			}
			return ObjectUtils.mapToJson(map);
		}
		return "";
	}
}
